layui.use(['table', 'jquery', 'layer'], function(){
    var $ = layui.$
    var table = layui.table
    var layer = layui.layer

    $('#mkdir').click(function(){
        layer.open({
            type: 1,
            title: '新建文件夹',
            btn: ['确定', '取消'],
            closeBtn: 0,
            content: '<div class="mkdir-layer"><div class="layui-form-item"><label class="layui-form-label">文件夹名称</label><div class="layui-input-inline"><input type="text" id="dirName" placeholder="请输入文件夹名称" class="layui-input"></div></div></div>',
            yes: function(){
                if($('#dirName').val() != ''){
                    $('#Content_Method').val('mkdir')
                    $('#Content_Value').val($('#dirName').val())
                    __doPostBack('ctl00$Content$doPost','')
                }else{
                    layer.msg('文件夹名称不能为空')
                }
            }
        })
    })
    $('#delfile').click(function(){
        let data = table.checkStatus('filelist').data
        if(data.length == 0){
            layer.msg('请选择需要删除的文件(夹)')
            return
        }
        layer.confirm('即将删除 '+ data.length +' 个文件(夹)，是否继续？', {closeBtn: 0, icon: 3, title:'批量删除'}, function(){
            let list = []
            for(i of data){
                list.push(i.id)
            }
            $('#Content_Method').val('delfile')
            $('#Content_Value').val(list.join(','))
            __doPostBack('ctl00$Content$doPost','')
        })
    })
    $('#mkshare').click(function(){
        let data = table.checkStatus('filelist').data
        if(data.length == 0){
            layer.msg('请选择需要分享的文件(夹)')
            return
        }
        layer.confirm('即将分享 '+ data.length +' 个文件(夹)，是否继续？', {closeBtn: 0, icon: 3, title:'批量分享'}, function(){
            let list = []
            for(i of data){
                list.push(i.id)
            }
            $('#Content_Method').val('mkshare')
            $('#Content_Value').val(list.join(','))
            __doPostBack('ctl00$Content$doPost','')
        })
    })
    table.on('tool', function(obj){
        let data = obj.data
        let filename = data.filename.replace(new RegExp(/<\/?.+?\/?>/g), '')
        if(obj.event === 'check'){
            if(data.type == 0) document.location.href = "/disk.aspx?path=" + data.id
        }else if(obj.event === 'down'){
            if(data.type != 0){
                layer.msg('正在准备下载')
                window.open('/download.aspx?file='+data.code);
            }else{
                layer.msg('文件夹不支持直接下载')
            }
        }else if(obj.event === 'del'){
            layer.confirm('确认删除文件(夹) "'+filename+'"？', {closeBtn: 0, icon: 3, title:'删除文件'}, function(){
                $('#Content_Method').val('delfile')
                $('#Content_Value').val(data.id)
                __doPostBack('ctl00$Content$doPost','')
            })
        }else if(obj.event === 'share'){
            layer.confirm('即将分享文件(夹) "'+filename+'"，是否继续？', {closeBtn: 0, icon: 3, title:'分享文件'}, function(){
                $('#Content_Method').val('mkshare')
                $('#Content_Value').val(data.id)
                __doPostBack('ctl00$Content$doPost','')
            })
        }
    })
})